package treeimport ()//XMLSpace is the W3C XML namespaceconstXMLSpace = "http://www.w3.org/XML/1998/namespace"//NodePos is a helper for representing the node's document ordertypeNodePosint//Pos returns the node's document order positionfunc ( NodePos) () int {returnint()}//NodeType is a safer way to determine a node's type than type assertions.typeNodeTypeint//GetNodeType returns the node's type.func ( NodeType) () NodeType {return}//These are all the possible node typesconst (NtAttrNodeType = iotaNtChdNtCommNtElemNtNsNtRootNtPi)//Node is a XPath result that is a node except elementstypeNodeinterface {//ResValue prints the node's string valueResValue() string//Pos returns the node's position in the document orderPos() int//GetToken returns the xml.Token representation of the nodeGetToken() xml.Token//GetParent returns the parent node, which will always be an XML elementGetParent() Elem//GetNodeType returns the node's typeGetNodeType() NodeType}//Elem is a XPath result that is an element nodetypeEleminterface {Node//GetChildren returns the elements children.GetChildren() []Node//GetAttrs returns the attributes of the elementGetAttrs() []Node}//NSElem is a node that keeps track of namespaces.typeNSEleminterface {ElemGetNS() map[xml.Name]string}//NSBuilder is a helper-struct for satisfying the NSElem interfacetypeNSBuilderstruct { NS map[xml.Name]string}//GetNS returns the namespaces found on the current element. It should not be//confused with BuildNS, which actually resolves the namespace nodes.func ( NSBuilder) () map[xml.Name]string {return .NS}type nsValueSort []NSfunc ( nsValueSort) () int { returnlen() }func ( nsValueSort) (, int) { [], [] = [], []}func ( nsValueSort) (, int) bool {return [].Value < [].Value}//BuildNS resolves all the namespace nodes of the element and returns themfunc ( Elem) ( []NS) { := make(map[xml.Name]string)if , := .(NSElem); {buildNS(, ) = make([]NS, 0, len()) := 1for , := range {if !(.Local == "xmlns" && .Space == "" && == "") { = append(, NS{Attr: xml.Attr{Name: , Value: },Parent: ,NodeType: NtNs, }) ++ } }sort.Sort(nsValueSort())for := range { [].NodePos = NodePos(.Pos() + + 1) } }return}func buildNS( NSElem, map[xml.Name]string) {if .GetNodeType() == NtRoot {return }if , := .GetParent().(NSElem); { (, ) }for , := range .GetNS() { [] = }}//NS is a namespace node.typeNSstruct {xml.Attr Parent ElemNodePosNodeType}//GetToken returns the xml.Token representation of the namespace.func ( NS) () xml.Token {return .Attr}//GetParent returns the parent node of the namespace.func ( NS) () Elem {return .Parent}//ResValue returns the string value of the namespacefunc ( NS) () string {return .Attr.Value}//GetAttribute is a convenience function for getting the specified attribute from an element.//false is returned if the attribute is not found.func ( Elem, , string) (xml.Attr, bool) { := .GetAttrs()for , := range { := .GetToken().(xml.Attr)if == .Name.Local && == .Name.Space {return , true } }returnxml.Attr{}, false}//GetAttributeVal is like GetAttribute, except it returns the attribute's value.func ( Elem, , string) (string, bool) { , := GetAttribute(, , )return .Value, }//GetAttrValOrEmpty is like GetAttributeVal, except it returns an empty string if//the attribute is not found instead of false.func ( Elem, , string) string { , := GetAttributeVal(, , )if ! {return"" }return}//FindNodeByPos finds a node from the given position. Returns nil if the node//is not found.func ( Node, int) Node {if .Pos() == {return }if , := .(Elem); { := .GetChildren()for := 1; < len(); ++ {if [-1].Pos() <= && [].Pos() > {return ([-1], ) } }iflen() > 0 {if [len()-1].Pos() <= {return ([len()-1], ) } } := .GetAttrs()for , := range {if .Pos() == {return } } := BuildNS()for , := range {if .Pos() == {return } } }returnnil}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.